home *** CD-ROM | disk | FTP | other *** search
-
- > I am trying to find out the dotted IP address of the machine to which a
- >socket is connected. My program listens for connection attempts and
- >acceptsthem, so I don't know the remote machine's address to begin with. I
- >can call 'getpeername' and have it fill a sockaddr structure, but I don't
- >know where to go from there. The sockaddr data structure consists of a
- >family field and a 14 byte address array according to the winsock header
- >file. I have tried calling 'inet_ntoa' with the address array to no avail.
- > Any ideas?
-
-
-
- struct sockaddr sad;
- struct in_addr sad_tmp;
- struct hostent *reqhostent;
- LPSTR szHostIP;
-
- LPSTR msg_buf[100];
- int len = sizeof(sad);
-
-
- /* take connection request off queue, and disable further
- notifications. we can process only one request at a time. */
-
- ConnectSocket = accept(ListenSocket, &sad, &len);
- WSAAsyncSelect(ListenSocket, hFrame, 0, 0);
-
- sad_tmp.S_un.S_un_b.s_b1 = sad.sa_data[2];
- sad_tmp.S_un.S_un_b.s_b2 = sad.sa_data[3];
- sad_tmp.S_un.S_un_b.s_b3 = sad.sa_data[4];
- sad_tmp.S_un.S_un_b.s_b4 = sad.sa_data[5];
-
- szHostIP = inet_ntoa( sad_tmp );
- reqhostent = gethostbyaddr( &sad_tmp, 4, PF_INET );
- if( NULL == reqhostent ){
- wsprintf( InfoBuf, (LPSTR) "Query from host %s, could not resolve
- the name.\n", szHostIP );
- } else {
- wsprintf( InfoBuf, (LPSTR) "Query from host %s, %s \n", szHostIP,
- reqhostent->h_name );
- }
-
-
-